/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package casino;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Vector;

/**
 * The wheel is a container of bins and is responsible for picking a winning bin.
 * 
 * @author  amaghfur
 * @since   1.0
 */
public class Wheel {
    
    /** number of bins in the wheel */
    public static final int NUM_OF_BINS = 38; 
    
    /** contains the individual bin instances */
    private Vector<Bin> Bins = new Vector<Bin>(NUM_OF_BINS);  
    
    /** random number generator to use to select the bin from the bins vector */
    protected Random rng = new Random();
    
    /** keeps track of individual outcome objects by name */
    private Map <String, Outcome> outcomeMap = new HashMap<>();
    
    /**
     * Constructor for wheel. Specify the random or non-random generator to be 
     * used to spin this wheel.
     * 
     */
    public Wheel(Random newRng) {
        // initialize bins
        for(int cnt = 0; cnt < NUM_OF_BINS; cnt++){
            Bins.addElement(new Bin());
        }
        
        rng = newRng;
    }
    
    /**
     * Constructor for wheel which uses the default random number generator.
     */
    public Wheel() {
        // initialize bins
        for(int cnt = 0; cnt < NUM_OF_BINS; cnt++){
            Bins.addElement(new Bin());
        }
        
        rng = new Random();
    }
    
    /**
     * Adds the given outcome to the bin with the given number and to the outcome map.
     * 
     * @param bin       bin number to be receiving the outcome
     */
    public void addOutcome(int bin, Outcome newOutcome) {
        Bins.get(bin).add(newOutcome);
        outcomeMap.put(newOutcome.getName(), newOutcome);
    }
    
    /**
     * Finds and returns the outcome in this wheel by its name.
     * 
     * @param outcomeName   the outcome name to be returned
     * @return              the outcome with the outcomeName
     */
    public Outcome getOutcome(String outcomeName) {
        return outcomeMap.get(outcomeName);
    }
    
    /**
     * Return a randomly selected bin.
     * 
     * @return  randomly selected bin
     */
    public Bin next() {
        return Bins.get(rng.nextInt(NUM_OF_BINS));
    }
    
    /**
     * Return the bin that corresponds to the specified number argument.
     * @param binNumber     number of the bin to be returned
     * @return              bin to be found
     */
    public Bin get(int binNumber) {
        return Bins.get(binNumber);
    }    
    
    /**
     * Get iterator for bins of this wheel.
     * 
     * @return  iterator for bins 
     */
    public Iterator<Bin> binIterator() {
        return Bins.iterator();
    }
    
}
